package com.HRs.UI;

import com.HRs.Controller.PatientController;
import com.HRs.Controller.aDController;
import com.HRs.domain.DoctorOrdered;
import com.HRs.domain.patientorder_info;
import com.HRs.utils.Account;
import com.HRs.utils.JDBCUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class Hall_Patient {
    JFrame frame = new JFrame("(用户:"+ Account.p_id+")预约挂号");
    JPanel panel1;
    JPanel panel2;
    JPanel panel3;

    DefaultTableModel tableModel_p;
    JTable table_p;
    String[][] table_Patient = new String[100][100];
    String[] columnId_P = {"科室","医生姓名","医生编号","预约时间"};

    DefaultTableModel tableModel_d;
    JTable table_d;
    String[][] table_Doctor = new String[100][100];
    String[] columnId_d = {"科室","医生姓名","医生编号","预约人数", "是否已满"};

    String department;
    String dp;

    JComboBox<String> boxName = new JComboBox<>();

    final int H = 770, W = 720;

    public Hall_Patient(){
        frame.setLayout(null);

        JPanel p1 = new JPanel();
        p1.setBounds(0, 0, 230 ,80);
        p1.setBackground(Color.DARK_GRAY);
        JLabel l1 = new JLabel("病人后台");
        l1.setFont(new Font("隶书", Font.PLAIN, 35));
        l1.setForeground(Color.WHITE);
        p1.add(BorderLayout.CENTER, l1);
        frame.add(p1);

        JPanel p2 = new JPanel();
        p2.setBounds(230, 0, 770, 80);
        p2.setBackground(Color.GRAY);
        JLabel l2 = new JLabel("预约挂号平台");
        l2.setFont(new Font("隶书", Font.PLAIN, 35));
        l2.setForeground(Color.WHITE);
        p2.add(l2);
        frame.add(p2);


        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(8, 1));
        p3.setBackground(Color.GRAY);
        p3.setBounds(0, 80, 230, 800);

//        JButton b_PInfo = new JButton("个人信息");
//        b_PInfo.setFont(new Font("隶书", Font.PLAIN, 25));
//        b_PInfo.setBounds(0, 0, 200, 50);
//        b_PInfo.setContentAreaFilled(false);
//        p3.add(b_PInfo);

        JButton b_Appointment = new JButton("预约挂号");
        b_Appointment.setFont(new Font("隶书", Font.PLAIN, 25));
        b_Appointment.setBounds(0, 0, 200, 50);
        b_Appointment.setContentAreaFilled(false);
        p3.add(b_Appointment);

        JButton b_HistoryInfo = new JButton("历史记录");
        b_HistoryInfo.setFont(new Font("隶书", Font.PLAIN, 25));
        b_HistoryInfo.setBounds(0, 50, 200, 50);
        b_HistoryInfo.setContentAreaFilled(false);
        p3.add(b_HistoryInfo);

        JButton b_CheckDoctorIsFull = new JButton("医生预约情况");
        b_CheckDoctorIsFull.setFont(new Font("隶书", Font.PLAIN, 25));
        b_CheckDoctorIsFull.setBounds(0, 100, 200, 50);
        b_CheckDoctorIsFull.setContentAreaFilled(false);
        p3.add(b_CheckDoctorIsFull);

        JButton b_Return = new JButton("返回登录界面");
        b_Return.setFont(new Font("隶书", Font.PLAIN, 25));
        b_Return.setBounds(0, 150, 200, 50);
        b_Return.setContentAreaFilled(false);
        b_Return.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Login();
                frame.dispose();
            }
        });
        p3.add(b_Return);
        frame.add(p3);

        Order();
        SearchHisInfo();
        DocOrderInfo();


        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getActionCommand().equals("预约挂号")){
                    System.out.println(e.getActionCommand());
                    panel3.setVisible(false);
                    panel2.setVisible(false);
                    panel1.setVisible(true);

                }else if(e.getActionCommand().equals("历史记录")){
                    System.out.println(e.getActionCommand());
                    panel3.setVisible(false);
                    panel1.setVisible(false);
                    panel2.setVisible(true);

                }else if (e.getActionCommand().equals("医生预约情况")){
                    System.out.println(e.getActionCommand());
                    panel1.setVisible(false);
                    panel2.setVisible(false);
                    panel3.setVisible(true);
                }
            }
        };

        b_Appointment.addActionListener(actionListener);
        b_HistoryInfo.addActionListener(actionListener);
        b_CheckDoctorIsFull.addActionListener(actionListener);

        frame.setSize(1000, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public void Order(){
        panel1 = new JPanel();
        panel1.setLayout(new GridLayout(6,1));
//        panel1.setLayout(new FlowLayout());
        panel1.setBounds(230, 80, H, W);
        JPanel t1 = new JPanel();
        JLabel KS = new JLabel("科    室:");
        KS.setFont(new Font("隶书", Font.PLAIN, 40));
//        panel1.add(KS);
        JComboBox<String> box = new JComboBox<>();
        box.addItem("---请选择科室---");
        aDController aD = new aDController();
        ArrayList<String> title = new ArrayList<>();
        title = aD.get_DpName();
        for(String na: title){
            box.addItem(na);
        }
        box.setFont(new Font("隶书", Font.PLAIN, 20));
        box.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dp = (String) box.getSelectedItem();
//                System.out.println(dp);
//                boxName = new JComboBox<>();
                PatientController PC = new PatientController();
                ArrayList<String> list_N = PC.get_DocName(dp);
                boxName.addItem("---"+dp+"---");
                for (String i : list_N){
                    boxName.addItem(i);
                }
            }
        });
//        panel1.add(box);
        t1.add(KS);
        t1.add(box);
        panel1.add(t1);

        JPanel t2 = new JPanel();
        JLabel docLbl = new JLabel("医生姓名:");
        docLbl.setFont(new Font("隶书", Font.PLAIN, 40));
//        JTextField txtDoc = new JTextField(20);
//        txtDoc.setFont(new Font("隶书", Font.ITALIC, 20));
//        JComboBox<String> boxName = new JComboBox<>();
        boxName.addItem("---请选择医生---");
//        PatientController PC = new PatientController();
//        ArrayList<String> list_N = PC.get_DocName(dp);
//        for (String i : list_N){
//            boxName.addItem(i);
//        }
        boxName.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        boxName.setFont(new Font("隶书", Font.PLAIN, 20));

        t2.add(docLbl);
//        t2.add(txtDoc);
        t2.add(boxName);
        panel1.add(t2);

        JPanel t5 = new JPanel();
        JLabel docId = new JLabel("医生编号:");
        docId.setFont(new Font("隶书", Font.PLAIN, 40));
        JTextField txtId = new JTextField(20);
        txtId.setFont(new Font("隶书", Font.ITALIC, 20));
        t5.add(docId);
        t5.add(txtId);
        panel1.add(t5);

        JPanel t3 = new JPanel();
        JLabel timeLbl = new JLabel("预约时间:");
        timeLbl.setFont(new Font("隶书", Font.PLAIN, 40));
//        panel1.add(docLbl);
        JTextField txtTime = new JTextField("格式为yyyy-MM-dd", 20);
        txtTime.setFont(new Font("隶书", Font.ITALIC, 20));
//        panel1.add(txtDoc);
        t3.add(timeLbl);
        t3.add(txtTime);
        panel1.add(t3);

        JPanel t4 = new JPanel();
        JButton b = new JButton("确定预约");
        b.setFont(new Font("隶书", Font.ITALIC, 30));
//        b.setBackground(Color.GRAY);
        b.setContentAreaFilled(false);

        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id, dp, doc, date,did;
                id = Account.p_id;
                dp = (String) box.getSelectedItem();
                doc = (String) boxName.getSelectedItem();
                date = txtTime.getText();
                did = txtId.getText();
                patientorder_info pi = new patientorder_info(id, dp, doc, date, did);
                PatientController PC = new PatientController();
                PC.Order_p(pi);
                if(isFull(did)){
                    JOptionPane.showMessageDialog(b, "预约成功！", "提示消息", JOptionPane.PLAIN_MESSAGE);
                }else {
                    JOptionPane.showMessageDialog(b, "预约失败！医生预约人数已满", "提示消息", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        t4.add(b);
        panel1.add(t4);

        frame.add(panel1);
//        panel1.setVisible(false);
    }

    public void SearchHisInfo(){
        panel2 = new JPanel();
        panel2.setBounds(230, 80, H, W);
        panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));

        get_his(table_Patient);

        JPanel t1 = new JPanel();
        tableModel_p = new DefaultTableModel(table_Patient, columnId_P);
        table_p = new JTable(tableModel_p);
        table_p.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        table_p.getTableHeader().setFont(new Font("隶书",Font.PLAIN,18));
        JScrollPane sc = new JScrollPane(table_p);
        t1.add(sc);
        panel2.add(t1);

        JPanel t2 = new JPanel();
        JButton b_Flush = new JButton("刷新");
        b_Flush.setFont(new Font("隶书", Font.ITALIC, 30));
        b_Flush.setContentAreaFilled(false);
        t2.add(b_Flush);
        b_Flush.addActionListener(new ActionListener() {//刷新
            @Override
            public void actionPerformed(ActionEvent e) {
                flush_his();
            }
        });
        panel2.add(t2);

        frame.add(panel2);
        panel2.setVisible(false);
    }

    public void DocOrderInfo(){
        panel3 = new JPanel();
        panel3.setBounds(230, 80, H, W);

        JPanel t1 = new JPanel();
        JLabel KS = new JLabel("科室:");
        KS.setFont(new Font("隶书", Font.PLAIN, 20));
//        panel1.add(KS);
        JComboBox<String> box = new JComboBox<String>();
        box.addItem("---请选择科室---");
        aDController aD = new aDController();
        ArrayList<String> title = new ArrayList<>();
        title = aD.get_DpName();
        for(String na: title){
            box.addItem(na);
        }

        box.setFont(new Font("隶书", Font.PLAIN, 20));
//        panel1.add(box);
        JLabel Dt = new JLabel("日期:");
        Dt.setFont(new Font("隶书", Font.PLAIN, 20));
        JTextField txtDt = new JTextField("格式为yyyy-MM-dd");
        txtDt.setFont(new Font("隶书", Font.PLAIN, 20));
        t1.add(KS);
        t1.add(box);
        t1.add(Dt);
        t1.add(txtDt);

        JButton b_Search = new JButton("查询");
        b_Search.setFont(new Font("隶书", Font.ITALIC, 20));
        b_Search.setContentAreaFilled(false);
        b_Search.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                flush_table();
                department = (String) box.getSelectedItem();
                flush_Update_Zd(department, txtDt.getText());
            }
        });

        t1.add(b_Search);
        panel3.add(t1);

        get_DocOrder(table_Doctor,department);
        tableModel_d = new DefaultTableModel(table_Doctor, columnId_d);
        table_d = new JTable(tableModel_d);
        table_d.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        table_d.getTableHeader().setFont(new Font("隶书",Font.PLAIN,18));
        JScrollPane sc = new JScrollPane(table_d);
        panel3.add(sc);

        frame.add(panel3);
        panel3.setVisible(false);
    }

    public void get_his(String[][] table_his){
        ArrayList<patientorder_info> po = new ArrayList<patientorder_info>();
        PatientController PC = new PatientController();
        po = PC.Search_hisInfo(Account.p_id);

        for(int i = 0; i < po.size(); i ++ ){
            table_his[i][0] = po.get(i).getPo_Department();
            table_his[i][1] = po.get(i).getPo_Doctor();
            table_his[i][3] = po.get(i).getPo_Date();
            table_his[i][2] = po.get(i).getPo_Id();
        }
    }

    public void flush_his(){
        String[][] table_F = new String[100][100];
        get_his(table_F);
        DefaultTableModel dtm = (DefaultTableModel) table_p.getModel();
        dtm.setDataVector(table_F, columnId_P);
        table_p.updateUI();
    }

    public void get_DocOrder(String[][] table_Doctor, String dp){
        ArrayList<DoctorOrdered> po = new ArrayList<>();
        PatientController PC = new PatientController();
        po = PC.Search_OrderInfo(dp);

        for(int i = 0; i < po.size(); i ++ ){
            table_Doctor[i][0] = po.get(i).getDo_Department();
            table_Doctor[i][1] = po.get(i).getDo_Name();
            table_Doctor[i][2] = po.get(i).getDo_Id();
            table_Doctor[i][3] = String.valueOf(po.get(i).getDo_Num());
            table_Doctor[i][4] = po.get(i).getDo_flag();
        }
    }

    public void get_DocOrder_Zd(String[][] table_Doctor, String dp, String dt){
        ArrayList<DoctorOrdered> po = new ArrayList<>();
        PatientController PC = new PatientController();
        po = PC.Search_OrderInfo_Zd(dp, dt);

        for(int i = 0; i < po.size(); i ++ ){
            table_Doctor[i][0] = po.get(i).getDo_Department();
            table_Doctor[i][1] = po.get(i).getDo_Name();
            table_Doctor[i][2] = po.get(i).getDo_Id();
            table_Doctor[i][3] = String.valueOf(po.get(i).getDo_Num());
            table_Doctor[i][4] = po.get(i).getDo_flag();
        }
    }

    public void flush_Update(String dp){
        String[][] table_F = new String[100][100];
        get_DocOrder(table_F, dp);

        DefaultTableModel dtm = (DefaultTableModel) table_d.getModel();
        dtm.setDataVector(table_F, columnId_d);
        table_d.updateUI();
    }

    public void flush_Update_Zd(String dp, String dt){
        String[][] table_F = new String[100][100];
        get_DocOrder_Zd(table_F, dp, dt);

        DefaultTableModel dtm = (DefaultTableModel) table_d.getModel();
        dtm.setDataVector(table_F, columnId_d);
        table_d.updateUI();
    }

    public void flush_table(){
        PatientController PC = new PatientController();
        PC.flush_SetZero_DocInfo();
        PC.flush_DocInfo();
    }

    public void setBoxName(){
        PatientController PC = new PatientController();
        ArrayList<String> list_N = PC.get_DocName(dp);
        for (String i : list_N){
            boxName.addItem(i);
        }
    }

    public boolean isFull(String id){
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        String tmp = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "select do_flag from doctorordered_info\n" +
                    "where do_Id = '"+id+"'";
            rs = st.executeQuery(sql);

            while (rs.next()){
                tmp = rs.getString("do_flag");

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }
        if(tmp.equals("是")) return false;
        else return true;
    }

    public static void main(String[] args) {
        Hall_Patient hallPatient = new Hall_Patient();
    }
}
